package com.honey.compilation.sql.ddl;

import java.util.ArrayList;
import java.util.List;

import com.honey.compilation.sql.SqlColumn;
import com.honey.compilation.sql.SqlKeyWord;
import com.honey.compilation.sql.SqlScript;
import com.honey.compilation.sql.SqlTable;
import com.honey.core.builder.CharacterBuilder;
import com.honey.core.builder.CharacterBuilderFactory;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.IndentSpace;
import com.honey.core.utils.StringUtility;

/**
 * 修改表命令: ALERT TABLE
 * @author Administrator
 *
 */
public  class AlterTableAddPrimaryKey extends AlterTableAddConstraint implements SqlScript{
	
	/** 列 */
	private List<String> columns ;
		
	/**
	 * 构造函数
	 * @param tableName 表的名称
	 */
	public AlterTableAddPrimaryKey(String tableName) {
		super(tableName);
		columns = new ArrayList<String>();
	}

	/**
	 * 构造函数
	 * @param table 数据库表对象
	 */
	public AlterTableAddPrimaryKey(SqlTable table){
		super(table);
		columns = new ArrayList<String>();
	}
	
	
	/**
	 * 构造函数
	 * @param tableName 表的名称
	 * @param columns 列
	 */
	public AlterTableAddPrimaryKey(String schema, String tableName){
		super(schema, tableName);
		columns = new ArrayList<String>();
	}
	
	/**
	 * 构造函数
	 * @param tableName 表的名称
	 * @param columns 列
	 */
	public AlterTableAddPrimaryKey(String tableName,SqlColumn  ...columns){
		this(tableName);
		addColumn(columns);
	}
	
	/**
	 * 添加字段
	 * @param columns
	 */
	public void addColumn(String ...columns){
		for(String column : columns)
			if(StringUtility.stringHasValue(column))this.columns.add(column);
	}

	/**
	 * 添加列
	 * @param columns
	 */
	public void addColumn(SqlColumn ...columns){
		for(SqlColumn column : columns)
			if(column != null)this.columns.add(column.getName().getValue());
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.core.dom.sql.ddl.AlterTableAddConstraint#compiledContent(int)
	 */
	@Override
	public CharacterBuilder compiledContent(int indentLevel) throws CompilationException {
		CharacterBuilder answer = CharacterBuilderFactory.createC16StringBuilder();

		boolean newLine = false;
		for(String column : columns ){
			if (newLine)IndentSpace.newLine(answer);else newLine = true;
			
			answer.append( super.compiledContent( indentLevel ) )/*; 
			answer.append( field ).append(SqlKeyWord.OPERATION_SPACE)*/
			.append( SqlKeyWord.PRIMARY).append( SqlKeyWord.KEY)
			.append( SqlKeyWord.OPERATION_PAREN_LEFT )
			.append( column )
			.append( SqlKeyWord.OPERATION_PAREN_RIGHT )
			.append(SqlKeyWord.OPERATION_SEMICOLON);
			;
		}
		return answer ;
	}

}
